﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using System.Collections;
using LayerEntity.Sistema;

namespace System2012.Listados
{
    public partial class frmListAnticipos : DevExpress.XtraEditors.XtraForm
    {
        private DataTable dtbCreditos;
        private DataTable dtbCuotas;
        string id_cliente;
        private int indexRow;

        public  string TOT_ANTICIPO;
        public string ID_CREDITO;
        public string ID_SUCURSAL;
        public int op_ok;
        public frmListAnticipos()
        {
            InitializeComponent();
        }
        public frmListAnticipos(ArrayList data)
        {
            InitializeComponent();
            id_cliente = data[2].ToString();
            txtIdCliente.Text = data[2].ToString();
            txtCliente.Text = data[5].ToString();
            txtDocCliente.Text = data[6].ToString();
            txtDirCliente.Text = data[7].ToString();
            if (!BGWCtasBrar.IsBusy) BGWCtasBrar.RunWorkerAsync();
        }

        private void BGWCtasBrar_DoWork(object sender, DoWorkEventArgs e)
        {
            ESistema obj = new ESistema();
            StringBuilder cadena = new StringBuilder();
            cadena.AppendFormat("where documento='ANTICIPO' AND id_cliente='{0}'", id_cliente);
            obj.Condicion = cadena.ToString();
            e.Result = System2012.SystemUtils.RecordManagerCreditos.getCtasporCobrar(obj);
        }

        private void BGWCtasBrar_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            dtbCreditos = (DataTable)e.Result;
            gridControl1.DataSource = dtbCreditos;// e.Result;
        }

        private void BGWCuotas_DoWork(object sender, DoWorkEventArgs e)
        {
            if (indexRow >= 0)
            {
                ESistema obj = new ESistema();
                StringBuilder cadena = new StringBuilder();
                cadena.AppendFormat("where id_credito='{0}'", dtbCreditos.Rows[indexRow]["id_credito"].ToString());
                cadena.AppendFormat("and id_sucursal_credito={0}", dtbCreditos.Rows[indexRow]["id_sucursal_credito"].ToString());
                obj.Condicion = cadena.ToString();
                e.Result = System2012.SystemUtils.RecordManagerCreditos.getCuotasPorCobrar(obj);
            }
        }

        private void gridView1_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
        {
            indexRow = e.FocusedRowHandle;
            this.indexRow = e.FocusedRowHandle;
           // opMov = 0;
            if (!BGWCtasBrar.IsBusy)
            {
                if (!BGWCuotas.IsBusy) BGWCuotas.RunWorkerAsync();

            }
        }

        private void BGWCuotas_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            dtbCuotas = (DataTable)e.Result;
            gridControl2.DataSource = e.Result;
        }

        private void btnGrabar_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            gridView1.FocusedColumn = gridView1.Columns["documento"];
            var select = gridView1.GetFocusedRowCellValue("selec");
            if (Convert.ToBoolean(select))
            {
                if (dtbCreditos.Rows[indexRow]["documento"].Equals("ANTICIPO"))
                {
                    this.TOT_ANTICIPO = gridView1.GetFocusedRowCellValue("credito").ToString();
                    this.ID_CREDITO = gridView1.GetFocusedRowCellValue("id_credito").ToString();
                    this.ID_SUCURSAL = gridView1.GetFocusedRowCellValue("id_sucursal_credito").ToString();
                 
                   // this.ID_CREDITO = dtbCuotas.Rows[indexRow]["id_credito"].ToString();
                    //this.ID_SUCURSAL = dtbCuotas.Rows[indexRow]["id_sucursal_credito"].ToString();
                    op_ok = 1;
                    this.Close();
                }
                else
                {
                    XtraMessageBox.Show(this, "Registro seleccionado Incorrecto\n"
                        + "no pertenece a un anticipo ", "AVISO", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
            else
            {
                XtraMessageBox.Show(this, "Seleccione el Registro de Anticipo" , "AVISO", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }

        private void btnSalir_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            op_ok = 0;
            Close();
        }
    }
}